/*
 * ImageUtils.java
 *
 *  created: 5.11.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Martin Pokorný
 * @version 0.1
 */
public class ImageUtils {

    /** */
    private ImageUtils() {
    }
    
    /**
     * 
     * @param imageFile
     * @return 
     */
    public static Dimension getImageSize(File imageFile) {
        return getImageSize(imageFile.getAbsolutePath());
    }

    /**
     * Získá velikost obrázku.
     *
     * @param imageFilePath
     * @return
     */
    public static Dimension getImageSize(String imageFilePath) {
        if (StringUtils.isBlank(imageFilePath)) {
            return null;
        }
        Dimension result = null;
        ImageIcon coverImage = new ImageIcon(imageFilePath);
        int width = coverImage.getImage().getWidth(null);
        int height = coverImage.getImage().getHeight(null);
        result = new Dimension(width, height);
        return result;
    }
    
    /**
     * 
     * (Nejsem autor, zkopírováno z internetu (?odkud))
     * 
     * @param icon
     * @return 
     */
    static Image iconToImage(Icon icon) {
        if (icon instanceof ImageIcon) {
            return ((ImageIcon)icon).getImage();
        } else {
            int w = icon.getIconWidth();
            int h = icon.getIconHeight();
            GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage image = gc.createCompatibleImage(w, h);
            Graphics2D g = image.createGraphics();
            icon.paintIcon(null, g, 0, 0);
            g.dispose();
            return image;
        }
    }    

}   // ImageUtils
